package org.saxion.aktiehuis.view;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.saxion.aktiehuis.model.Klant;
import org.saxion.aktiehuis.model.Product;
import org.saxion.aktiehuis.model.ProductSoort;
import org.saxion.aktiehuis.services.BestellingManagement;
import org.saxion.aktiehuis.services.KlantManagement;
import org.springframework.beans.factory.annotation.Autowired;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class MainView {

	private JFrame frame;
	private GetNameDialog getNameDialog;
	private String klantnaam = "";
	private int bestelId;
	
	private ProductenView productenView;
	private OrderView orderView;
	private WinkelWagenView winkelWagenView;
	
	private BestellingManagement bestellingManagement;
	private KlantManagement klantManagement;
	private int klantId;

	//private SessionFactory sessionfactory;
	/**
	 * Create the application.
	 * @throws Exception 
	 */
	public MainView(){

		initialize();
		

	}
	
	public void start() 
	{
		
		
		productenView.showProducts();
		productenView.getProductSoorten();
		changePanel(productenView);
		frame.setVisible(true);
		checkKlantnaam();

	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		//dialog
		getNameDialog = new GetNameDialog();
		getNameDialog.addConfirmListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				checkKlantnaam();
			}
		});
		
		
		//jframe
		frame = new JFrame();
		frame.setBounds(100, 100, 675, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnNewMenu = new JMenu("Menu");
		menuBar.add(mnNewMenu);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("Producten");
		JMenuItem mntmWinkelwagen = new JMenuItem("Winkelwagen");
		JMenuItem mntmOrderOverzicht = new JMenuItem("Order overzicht");

		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					productenView.showProducts(); //tijdelijk!
					productenView.getProductSoorten();
					changePanel(productenView);
					
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		
		mntmWinkelwagen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				winkelWagenView.showBestelling();
				changePanel(winkelWagenView);
			}
		});
		
		mntmOrderOverzicht.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				orderView.getOrders();
				changePanel(orderView);
			}
		});
		
		mnNewMenu.add(mntmNewMenuItem);
		mnNewMenu.add(mntmWinkelwagen);
		mnNewMenu.add(mntmOrderOverzicht);
		
		frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
	}
	
	
	private void changePanel(JPanel panel)
	{
		try {
			if(bestellingManagement.getBestelling(bestelId) != null &&  bestellingManagement.getBestelling(bestelId).getBevestigd()){
				bestelId = bestellingManagement.getBestelNr(klantnaam);
				
				productenView.setBestelId(bestelId);
				winkelWagenView.setBestelId(bestelId);
			}
				
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		frame.getContentPane().removeAll();
		frame.getContentPane().add(panel);
		frame.validate();
		frame.paint(frame.getGraphics());
	}
	
	
	private void checkKlantnaam()
	{
		klantnaam = getNameDialog.getKlantNaam();
		if(klantnaam.equals("")){
			getNameDialog.setVisible(true);
		}
		else
		{
			try {
				klantId = klantManagement.getKlantId(klantnaam);
				
				bestelId = bestellingManagement.getBestelNr(klantnaam);
				
				productenView.setBestelId(bestelId);
				winkelWagenView.setBestelId(bestelId);
				
				productenView.setKlantId(klantId);
				winkelWagenView.setKlantId(klantId);
				orderView.setKlantId(klantId);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			getNameDialog.setVisible(false);
		}
	}
	
	public void setProductenView(ProductenView productenView) throws Exception
	{
		this.productenView = productenView;
	}
	
	public void setOrderView(OrderView orderView) throws Exception
	{
		this.orderView = orderView;
	}
	
	public void setWinkelWagenView(WinkelWagenView winkelWagenView) throws Exception
	{
		this.winkelWagenView = winkelWagenView;
	}
	
	public void setBestellingManagement(BestellingManagement bestellingManagement) throws Exception
	{
		this.bestellingManagement =bestellingManagement;
	}
	
	public void setKlantManagement(KlantManagement klantManagement) throws Exception
	{
		this.klantManagement =klantManagement;
	}
	

	
}
